package com.zhxh.nzpos.login;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Editable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zhxh.nzpos.BaseActivity;
import com.zhxh.nzpos.ICountService;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.ReadCardService;
import com.zhxh.nzpos.TestData;
import com.zhxh.nzpos.utils.HexUtils;

/**
 * 操作员登录界面，登录后进入主界面@MainActivity
 * 
 * @author wangzt
 * 
 */
public class LoginActivity extends BaseActivity {

	String cardId = null;
	
	public static final String TAG = "LoginActivity";
	
	public static final String PREFS_NAME = "PreferencesFile";
	public static final String CARD_ID = "cardId";
	public static final String NUMBER = "number";
	public static final String USER = "user";
	public static final String INFORMATION = "dianzhuinfo";

	public static final String PASSWORD = "password";

	public static final String ADMIN = "admin";
	private EditText cardET;
	private String mPassword;

	private EditText mPasswordET;
	CardReceiver cardReceiver;

	private ICountService countService;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_opetator_login);
		initViews();
	
	}

	private void initViews() {
		// Intent intent = getIntent();
		// String card =
		// TestData.OPERATOR_CARD;//intent.getStringExtra(Constants.OPERATOR_CARD);
		cardET = (EditText) findViewById(R.id.operator_card_et);
		// cardET.setText(card);

		mPasswordET = (EditText) findViewById(R.id.operator_password_et);

		mPassword = TestData.OPERATOR_PASSWORD; // intent.getStringExtra(Constants.OPERATOR_PASSWORD);

		Button okBtn = (Button) findViewById(R.id.operator_login_ok_btn);
		okBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Editable et = mPasswordET.getText();
				if (et != null) {
					String password = et.toString();
					if (password != null && password.equals(mPassword)) {
						unbindService(serviceConnection);
						Intent intent = new Intent();
						intent.setClass(getApplicationContext(),
								MainActivity.class);

						startActivity(intent);
						stopService(new Intent(LoginActivity.this,
								ReadCardService.class));
						// finish();
					} else {
						Toast.makeText(getApplicationContext(),
								getString(R.string.password_error),
								Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(getApplicationContext(),
							getString(R.string.password_blank),
							Toast.LENGTH_SHORT).show();
				}

				// 测试使用
				/*
				 * Intent intent = new Intent();
				 * intent.setClass(getApplicationContext(), MainActivity.class);
				 * startActivity(intent); finish();
				 */
			}
		});

		/*
		 * Button cancelBtn = (Button)
		 * findViewById(R.id.operator_login_cancel_btn);
		 * cancelBtn.setOnClickListener(new OnClickListener() { public void
		 * onClick(View v) { Intent intent = new Intent();
		 * intent.setClass(getApplicationContext(), LoginActivity.class);
		 * startActivity(intent); finish(); } });
		 */
	}


	private void setPreferences(String key, String mPreferences) {
		// set preference
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(key, mPreferences);
		editor.commit();
	}

	@Override
	protected void onStart() {

		cardET.setText("");
		mPasswordET.setText("");
		System.out.println("#####################LoginActivity######################################onStart");
		//startService(new Intent(this, ReadCardService.class));// 开始启动服务
		bindService(new Intent("com.zhxh.nzpos.ReadCardService"), serviceConnection,
				BIND_AUTO_CREATE);
		RegBroadcast();// 注册广播
		super.onStart();
	}

	public void RegBroadcast()

	{
		cardReceiver = new CardReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction("android.intent.action.zhhxh.readcard");
		registerReceiver(cardReceiver, filter);

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		System.out.println("#####################LoginActivity######################################onDestroy");
	  // stopService(new Intent(this, ReadCardService.class));
		unbindService(serviceConnection);
		unregisterReceiver(cardReceiver);
		super.onDestroy();
	}

	public class CardReceiver extends BroadcastReceiver {

		// 自定义一个广播接收器，内部广播

		@Override
		public void onReceive(Context context, Intent intent) {

			// TODO Auto-generated method stub

			System.out.println("OnReceiver");
			Bundle bundle = intent.getExtras();
			String idcard1 = bundle.getString("idcard1");
			String idcard2 = bundle.getString("idcard2");
			String card = bundle.getString("card");
			String dianzhuInfo = bundle.getString("dianzhuInfo");
			String password = bundle.getString("password");
			String number = bundle.getString("number");

			//System.out.println("1111111=" + HexUtils.removeZero(dianzhuInfo));
			
			cardET.setText(HexUtils.removeZero(dianzhuInfo));
			mPassword = HexUtils.removeZero(password);
			System.out.println("#########################################333");
			System.out.println("idcard1=" + idcard1);
			System.out.println("idcard2=" + idcard2);
			
			System.out.println("cardET=" + cardET.getText().toString());
			System.out.println("mPassword=" + mPassword);
			
			System.out.println("card=" + card);
			System.out.println("dianzhuInfo=" + dianzhuInfo);
			System.out.println("password=" + password);
			System.out.println("number=" + number);
			System.out.println("#########################################444");
			// pd.setProgress(a);
			
			setPreferences(CARD_ID, cardET.getText().toString());
			setPreferences(NUMBER, number.substring(0,3));

		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			Log.i(TAG, "KEYCODE_BACK");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {

	/*		countService = (ICountService) service;

			Log.v(" CountService ", " on serivce connected, count is "

			+ countService.getCount());*/

		}

		@Override
		public void onServiceDisconnected(ComponentName name) {

			countService = null;

		}

	};

}
